package com.zuxia.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zuxia.entity.Message;
import com.zuxia.service.IMessageService;

@Controller("messageAction")
@Scope("prototype")//定义单例或多例
@ParentPackage("json-default")
@Namespace("/")
@Results({
	@Result(name="select",type="dispatcher",location="/admin/message.jsp"),
	@Result(name="selectMessageId",type="dispatcher",location="/admin/user_show.jsp"),
	@Result(name="selectTime",type="dispatcher",location="/feedBackDetail.jsp"),
	@Result(name="updateMessage",type="dispatcher",location="/admin/message.jsp"),
	@Result(name="json",type="json")
})
public class MessageAction extends ActionSupport implements ModelDriven<Message>{
	private static final long serialVersionUID = 1L;
	
	private Message message = new Message();
	private List<Message> list;
	private String checks;//用于做批量删除 
	private boolean bol;

	@JSON(serialize=false)
	public List<Message> getList() {
		return list;
	}
	public void setList(List<Message> list) {
		this.list = list;
	}
	@JSON(serialize=false)
	public String getChecks() {
		return checks;
	}
	public void setChecks(String checks) {
		this.checks = checks;
	}
	public Message getModel() {
		return message;
	}
	
	public boolean isBol() {
		return bol;
	}
	public void setBol(boolean bol) {
		this.bol = bol;
	}

	@Resource(name="messageService")
	private IMessageService messageService;
	
	@Action("selectMessage")
	public String selectMessage(){
		list = messageService.selectAll();
		ActionContext.getContext().put("list", list);
		return "select";
	}
	
	@Action("selectMessageId")
	public String selectId() {
		try {
			this.message = messageService.selectId(message.getMessage_id());
			return "selectMessageId";
		} catch (Exception e) {
			return "error";
		}
	}
	
	@Action("deleteMessage")
	public String deleteMessage() {
		bol = messageService.deleteMessage(message);
		ActionContext.getContext().getValueStack().push(bol);
		return "json";
	}
	
	@Action("batcheDeleteMessage")
	public String batcheDeleteMessage() {
		bol = messageService.batchDelteMessage(checks);
		ActionContext.getContext().getValueStack().push(bol);
		return "json";
	}
	
	@Action("saveMessage")
	public String saveMessage() {
		bol = messageService.saveMessage(message);
		ActionContext.getContext().getValueStack().push(bol);
		return "json";
	}
	
	@Action("selectTime")
	public String selectTime(){
		list = messageService.selectTime();
		ActionContext.getContext().getValueStack().push(list);
		return "json";
	}
	
	@Action("updateMessage")
	public String updateMessage() {
		return messageService.updateMessage(message);
	}
}
